package com.xxx.test;

import com.xxx.DAO.booksDao;
import com.xxx.DAO.user_bookDao;
import com.xxx.entity.Book;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


@WebServlet("/ajaxBorrow")
public class ajaxBorrow extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json="{}";
        int userid = (int) request.getSession().getAttribute("id");
        String id=request.getParameter("id");
        booksDao bDao=new booksDao();
        user_bookDao ubDao=new user_bookDao();
        ubDao.add(userid,id);
        bDao.updateSum(id);
        Book book=bDao.selectId(id);
        int sum=book.getbSum();
        json="{\"sum\":"+sum+",\"string\":\"已借\"}";
        response.setContentType("application/json;charset=utf-8");
        PrintWriter pw=response.getWriter();
        pw.println(json);//输出数据，会赋值给Ajax中responseText属性
        pw.flush();
        pw.close();

    }
}
